package com.huluobo.a2303mdmdemo.video.pages

import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2303mdmdemo.video.VideoIntent
import com.huluobo.a2303mdmdemo.video.VideoViewModel
import com.huluobo.lib_base.room.VideoEntity
import com.huluobo.lib_base.room.VideoEntityItem
import com.huluobo.lib_base.BaseLoadingDialog
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_base.UIState
import com.huluobo.lib_http.Const
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/8/29.
 */

@Composable
fun VideoListPage(navHostController: NavHostController, vm: VideoViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<VideoEntityItem>()
    }

    var isShowProgress by remember {
        mutableStateOf(false)
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnFailed -> {
                        isShowProgress = false
                        ToastUtils.showLong("获取视频列表失败:${it.msg}")
                        Log.e("TagA", "获取视频列表失败:${it.msg}")
                    }

                    UIState.OnLoading -> {
                        isShowProgress = true
                    }

                    is UIState.OnSuccess<*> -> {
                        isShowProgress = false
                        list.clear()
                        list.addAll(it.data as VideoEntity)
                        Log.e("TagA", "videos:${list.size}")
                    }
                }
            }
        }
        vm.sendIntent(VideoIntent.GetAllVideos(1, 50))
    })

    if (isShowProgress) {
        BaseLoadingDialog()
    }

    Column(Modifier.padding(20.dp)) {
        Text(text = "视频", fontSize = 26.sp, fontWeight = FontWeight.Bold)
        Spacer(modifier = Modifier.height(10.dp))
        LazyVerticalStaggeredGrid(columns = StaggeredGridCells.Fixed(2), content = {
            items(list) {
                VideoListItem(entityItem = it, navHostController = navHostController)
            }
        }, verticalItemSpacing = 5.dp, horizontalArrangement = Arrangement.Center)
    }
}

@Composable
fun VideoListItem(entityItem: VideoEntityItem, navHostController: NavHostController) {
    Card(
        border = BorderStroke(1.dp, Color.Gray), colors = CardDefaults.cardColors(containerColor = Color.White),
        modifier = Modifier
            .clickable {
                val json = Gson().toJson(entityItem)
                MMKV
                    .defaultMMKV()
                    .encode(Const.KEY_VIDEO, json)
                navHostController.navigate(RoutePath.VIDEO_PAGER)
            }
            .padding(5.dp)
    ) {
        Column(
            Modifier
                .padding(5.dp)
                .fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(5.dp)
        ) {
            AsyncImage(
                model = entityItem.videomainimg, contentDescription = "", modifier = Modifier
                    .fillMaxWidth()
                    .clip(
                        RoundedCornerShape(topStart = 5.dp, topEnd = 5.dp)
                    ),
                contentScale = ContentScale.Crop
            )
            Row(verticalAlignment = Alignment.CenterVertically) {
                AsyncImage(
                    model = entityItem.headpath, contentDescription = "", modifier = Modifier
                        .size(30.dp)
                        .clip(
                            CircleShape
                        )
                )
                Spacer(modifier = Modifier.width(10.dp))
                Text(text = entityItem.authname, maxLines = 1, overflow = TextOverflow.Clip)
            }
        }
    }
}